<template>
  <div>
    
    <vol-box :model.sync="model" :padding="30" title="会员发券" :width="950" :height="600">
 
        <VolForm ref="myform" :loadKey="loadKey" :formFileds="formFileds" :formRules="formRules"></VolForm>
      
      <div slot="footer">
          <Button type="success" @click="getFirstRow">保存</Button>
        <Button
          type="info"
          size="large"
          icon="md-close"
          @click="()=>{this.model=false}"
        >关闭</Button>
      </div>
      
     
    </vol-box>
  </div>
</template>
<script>
import VolForm from "@/components/basic/VolForm.vue";
export default {
    
  components: {
    VolBox: () => import("@/components/basic/VolBox.vue"),
    VolForm,



  },
  data() {
    return {
      loadKey: true,
      row: {},
      url: "api/App_Expert/getPageData", //后从加载数据的url
      model: false,
      pagination: {
        total: 0, //分页总数量
        size: 30, //分页大小,30,60,100.
        sortName: "CreateDate" //从后加载数据分页时的排序字段
      },
      formFileds: {
       //发券
       "volumeName":"",
       "ticketNumber":"",
       "ticketLength":"",
       "volumePrice":"",
       "ticketPrefix":"",
       "ticketSuffix":"",
       "ticketStartNumber":"",
       "effectiveStartDate":"",
       "effectiveEndDate":"",
       "batchNumber":"",
       "remark":"",
       "volumeID":"",
       "random":""
        



      },
      formRules: [
        //两列的表单，formRules数据格式为:[[{},{}]]
        //发券
      
        [{"title":"券描述","field":"volumeName","type":"text",disabled:true},
        {"title":"数量","required":true,"field":"ticketNumber","type":"number"}],
        [{"title":"票号长度","required":true,"field":"ticketLength","type":"number"},
        {"title":"价格","field":"volumePrice","type":"text",disabled: true}],
        [{"title":"票号前缀","field":"ticketPrefix","type":"text"},
        {"title":"票号后缀","field":"ticketSuffix","type":"text"}],
        [{"title":"起始票号","field":"ticketStartNumber","required":true,"type":"number"},
         {"title":"批次号","field":"batchNumber","type":"text","required":true},],
        [ {"title":"有效日期从","field":"effectiveStartDate","type":"datetime","required":true},
        {"title":"到","field":"effectiveEndDate","type":"datetime","required":true},],
        [{"title":"是否随机生成券","field":"random","type":"switch"},
       ],
        [{"title":"备注","field":"remark","type":"textarea",colSize:12}]],
         
        
    };
  
  },
  methods: {
    open(row) {
      this.$store.getters.data().viewPageTest=this;
      this.row = row;
      this.model = true;
       var ticketNumber = this.getFormOption('ticketNumber');
       ticketNumber.max=row.quantity;
        //var onlineCode = this.getFormOption('onlineCode');
      //发券
      
      this.formFileds.volumeID=row.id;
      this.formFileds.volumeName=row.volumeDescription;
      this.formFileds.volumePrice=row.ticketPrice;
      this.formFileds.ticketStartNumber=row.ticketStartNumber;
      this.formFileds.effectiveStartDate=row.effectiveStartDate;
      this.formFileds.effectiveEndDate=row.effectiveEndDate;
      this.formFileds.batchNumber=new Date().getFullYear().toString()+(new Date().getMonth()+1).toString()+new Date().getDate().toString()+new Date().getHours().toString()+new Date().getMinutes().toString()+new Date().getSeconds().toString()+new Date().getMilliseconds().toString()+row.id;
      this.formFileds.random=row.random;
       
      
     
        var random = this.getFormOption('random');
        // var ticketPrefix = this.getFormOption('ticketPrefix');
        // var ticketSuffix = this.getFormOption('ticketSuffix');
        // var ticketStartNumber = this.getFormOption('ticketStartNumber');
       if(random==1){
         random.onInput=(val)=>{
          this.editFormFields.ticketPrefix=this.base.chineseToPinYin(val);
        }
         var ticketPrefix = this.getFormOption('ticketPrefix');
         this.$set(ticketPrefix, "disabled", true)
          var ticketSuffix = this.getFormOption('ticketSuffix');
           this.$set(ticketSuffix, "disabled", true)
          var ticketStartNumber = this.getFormOption('ticketStartNumber');
            this.$set(ticketStartNumber, "disabled", true)
          var ticketLength = this.getFormOption('ticketLength');
          this.$set(ticketLength,"disabled",true)
       }
       random.onChange = (val,item)=>{
        var ticketPrefix = this.getFormOption('ticketPrefix');
        var ticketSuffix = this.getFormOption('ticketSuffix');
        var ticketStartNumber = this.getFormOption('ticketStartNumber');
        var ticketLength = this.getFormOption('ticketLength');
        if(val==1){
        
          this.formFileds.ticketStartNumber = new Date().getSeconds().toString()+new Date().getMilliseconds().toString()+row.id;
        }
         if(val == 0){
             //this.editFormFields.ticketPrefix='';
             this.$set(ticketPrefix,"disabled",false)
             this.$set(ticketPrefix,"required",true)
         }
         else 
         {
              this.$set(ticketPrefix,"disabled",true)
              this.$set(ticketPrefix,"required",false)
         }
         if(val == 0){
             //this.editFormFields.ticketSuffix='';
             this.$set(ticketSuffix,"disabled",false)
             this.$set(ticketSuffix,"required",true)
         }else 
         {
              this.$set(ticketSuffix,"disabled",true)
              this.$set(ticketSuffix,"required",false)
         }
         if(val == 0){
             //this.editFormFields.ticketStartNumber='';
             this.$set(ticketStartNumber,"disabled",false)
             this.$set(ticketStartNumber,"required",true)
         }else {
              this.$set(ticketStartNumber,"disabled",true)
              this.$set(ticketStartNumber,"required",false)
         }
        if(val == 0){
             //this.editFormFields.ticketPrefix='';
             this.$set(ticketLength,"disabled",false)
             this.$set(ticketLength,"required",true)
         }
         else 
         {
              this.$set(ticketLength,"disabled",true)
              this.$set(ticketLength,"required",false)
         }
      } 
    },
    getFormOption(field) {
        let option;
        this.formRules.forEach(x => {
            x.forEach(item => {
                if (item.field == field) {
                    option = item;
                }
            })
        })
        return option;
        },
    getForm() {
      if(!this.myform.validate()){
        return;
      }
      this.$message.error(json.stringify(this.formFileds));
      this.$refs.accountAdd.model = true;
      this.$refs.accountAdd.formFileds.accountName=this.formFileds.accountsReceivable;
      //this.$refs.accountAdd.formFileds.accountCode=this.formFileds.accountsReceivableCode;
      this.$refs.accountAdd.formFileds.financeRemark=this.formFileds.financeRemark;
      //发券
     // this.$refs.volume_TypeAdd.formFileds.volume_TypeName=this.formFileds.volumeType;
    },
    getForm1() {
      this.$refs.Income.model = true;
      this.$refs.Income.formFileds.accountName=this.formFileds.accountsReceivable;
      //this.$refs.Income.formFileds.accountCode=this.formFileds.accountsReceivableCode;
      this.$refs.Income.formFileds.financeRemark=this.formFileds.financeRemark;
      //发券
     // this.$$refs.volume_TypeAdd.formFileds.volume_TypeName=this.formFileds.volumeType;
    
    },
    
    getFirstRow() {
        let formData = {
            mainData:this.formFileds,
            detailData: null,
            delKeys: null
            };
        this.http.post("/api/mall_volume_item/Add", formData, true).then(x => {
        if (!x.status) return this.$message.error(x.message);
        this.$message.success(x.message);
        this.model=false;
        let data = { startingTime: new Date(), };
        this.$refs.myform.reset(data);
       this.close();
      // this.refresh();
    });
    },
    reset() {
    },
  },
  created() {},
};
</script>
<style lang="less" scoped>
h3 {
  font-weight: 500;
  > span:last-child {
    margin-left: 30px;
  }
}
</style>